package com.rdc.qingkou.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.rdc.qingkou.Application.MyApplication;
import com.rdc.qingkou.R;

import java.util.ArrayList;
import java.util.List;

public class WelcomeActivity extends AppCompatActivity {

    private ViewPager mGuideViewPager;
    private LinearLayout mGrayDotsLayout;
    private List<View> mViewList;
    private ImageView mDeepGreen_dotsImageView;
    private int mDistance;
    private ImageView mOne_dotImageView;
    private ImageView mTwo_dotImageView;
    private ImageView mThree_dotImageView;
    private ImageView mFour_dotImageView;
    private Button mStartButton;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MyApplication.addActivity(this);
        Window window = getWindow();
        //隐藏标题栏
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        //隐藏状态栏
        //定义全屏参数
        int flag = WindowManager.LayoutParams.FLAG_FULLSCREEN;
        //设置当前窗体为全屏显示
        window.setFlags(flag, flag);
        setContentView(R.layout.activity_welcome);
        initData();
        initView();
        addDots();
        moveDots();
    }

    public void initView() {
        mGuideViewPager = (ViewPager) findViewById(R.id.welcome_viewpager);
        mGuideViewPager.setAdapter(new GuideViewPagerAdapter());
        mGrayDotsLayout = (LinearLayout) findViewById(R.id.welcome_lightGreen_dots_linearLayout);
        mDeepGreen_dotsImageView = (ImageView) findViewById(R.id.welcome_deepGreen_dot_imageView);
        mStartButton = (Button) findViewById(R.id.welcome_start_Button);
        mStartButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
                startActivity(new Intent(WelcomeActivity.this, LoginActivity.class));
            }
        });
    }

    private void initData() {
        mViewList = new ArrayList<>();
        LayoutInflater lf = getLayoutInflater().from(WelcomeActivity.this);
        View view1 = lf.inflate(R.layout.welcome1, null);
        View view2 = lf.inflate(R.layout.welcome2, null);
        View view3 = lf.inflate(R.layout.welcome3, null);
        View view4 = lf.inflate(R.layout.welcome4, null);
        mViewList.add(view1);
        mViewList.add(view2);
        mViewList.add(view3);
        mViewList.add(view4);
    }

    private void addDots() {
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.setMargins(0, 0, 40, 0);
        mOne_dotImageView = new ImageView(this);
        mOne_dotImageView.setImageResource(R.drawable.light_green_dot);
        mGrayDotsLayout.addView(mOne_dotImageView, layoutParams);
        mTwo_dotImageView = new ImageView(this);
        mTwo_dotImageView.setImageResource(R.drawable.light_green_dot);
        mGrayDotsLayout.addView(mTwo_dotImageView, layoutParams);
        mThree_dotImageView = new ImageView(this);
        mThree_dotImageView.setImageResource(R.drawable.light_green_dot);
        mGrayDotsLayout.addView(mThree_dotImageView, layoutParams);
        mFour_dotImageView = new ImageView(this);
        mFour_dotImageView.setImageResource(R.drawable.light_green_dot);
        mGrayDotsLayout.addView(mFour_dotImageView, layoutParams);
    }

    private void moveDots() {
        mDeepGreen_dotsImageView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                //获取两个圆点之间的距离
                mDistance = mGrayDotsLayout.getChildAt(1).getLeft() - mGrayDotsLayout.getChildAt(0).getLeft();
                mDeepGreen_dotsImageView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            }
        });
        mGuideViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                //页面滚动时小白点移动，并通过setLayoutParams(params)不断更新其位置
                float leftMargin = mDistance * (position + positionOffset);
                RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) mDeepGreen_dotsImageView.getLayoutParams();
                params.leftMargin = (int) leftMargin;
                mDeepGreen_dotsImageView.setLayoutParams(params);
                if (position == 3) {
                    mStartButton.setVisibility(View.VISIBLE);
                }
                if (position != 3 && mStartButton.getVisibility() == View.VISIBLE) {
                    mStartButton.setVisibility(View.GONE);
                }
            }

            @Override
            public void onPageSelected(int position) {
                //页面跳转时，设置小圆点的margin
                float leftMargin = mDistance * position;
                RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) mDeepGreen_dotsImageView.getLayoutParams();
                params.leftMargin = (int) leftMargin;
                mDeepGreen_dotsImageView.setLayoutParams(params);
                if (position == 3) {
                    mStartButton.setVisibility(View.VISIBLE);
                }
                if (position != 3 && mStartButton.getVisibility() == View.VISIBLE) {
                    mStartButton.setVisibility(View.GONE);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }

    //ViewPager的适配器
    private class GuideViewPagerAdapter extends PagerAdapter {
        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(mViewList.get(position));
            return mViewList.get(position);
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(mViewList.get(position));
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public int getCount() {
            return mViewList.size();
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        finish();
    }
}
